<?php

namespace Agp\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\FormInterface;
use APY\DataGridBundle\Grid\Mapping as GRID;
use Agp\CoreBundle\Entity\Telefone;

/**
 * Agp\CoreBundle\Entity\Empresa
 *
 * @ORM\Table(name="empresa")
 * @ORM\Entity(repositoryClass="Agp\CoreBundle\Repository\EmpresaRepository")
 * @GRID\Source(columns="codEmpresa, razaoSocial, cidade.nome, situacao")
 * @ORM\HasLifecycleCallbacks
 */
class Empresa
{
    /**
     * @var integer $codEmpresa
     *
     * @ORM\Column(name="cod_empresa", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @GRID\Column(title="Código", type="text", size="5", primary=true, visible=false)
     */
    protected $codEmpresa;
    
    /**
     *
     * @ORM\OneToMany(targetEntity="TerminalEmpresa", mappedBy="empresa", cascade={"all"})
     * @ORM\OrderBy({"codTerminalEmpresa" = "ASC"})
     */
    protected $empresaReference;
    
    /**
     * @var string $razaoSocial
     *
     * @ORM\Column(name="razao_social", type="string", length=45, nullable=false)
     * @Assert\NotBlank
     * 
     * @GRID\Column(title="Razão Social", type="text", size="400")
     */
    protected $razaoSocial;

    /**
     * @var string $fantasia
     *
     * @ORM\Column(name="fantasia", type="string", length=45, nullable=true)
     */
    protected $fantasia;

    /**
     * @var string $cnpj
     *
     * @ORM\Column(name="cnpj", type="string", length=45, nullable=true)
     * @Assert\NotBlank(groups={"juridica"})
     */
    protected $cnpj;
    
    /**
     * @var string $cnpj
     *
     * @ORM\Column(name="cpf", type="string", length=45, nullable=true)
     * @Assert\NotBlank(groups={"fisica"})
     */
    protected $cpf;

    /**
     * @var string $ie
     *
     * @ORM\Column(name="ie", type="string", length=45, nullable=true)
     */
    protected $ie;

    /**
     * @var string $im
     *
     * @ORM\Column(name="im", type="string", length=45, nullable=true)
     */
    protected $im;

    /**
     * @var string $endereco
     *
     * @ORM\Column(name="endereco", type="string", length=45, nullable=false)
     * @Assert\NotBlank
     */
    protected $endereco;

    /**
     * @var string $complemento
     *
     * @ORM\Column(name="complemento", type="string", length=45, nullable=true)
     */
    protected $complemento;

    /**
     * @var string $cep
     *
     * @ORM\Column(name="cep", type="string", length=45, nullable=false)
     * @Assert\NotBlank
     */
    protected $cep;

    /**
     * @var string $bairro
     *
     * @ORM\Column(name="bairro", type="string", length=45, nullable=false)
     * @Assert\NotBlank
     */
    protected $bairro;

    /**
     * @var integer $cidade
     *
     * @ORM\ManyToOne(targetEntity="Cidade", cascade={"persist"}, fetch="LAZY")
     * @ORM\JoinColumn(name="cod_cidade", referencedColumnName="id", onDelete="restrict", nullable=false)
     * @Assert\NotBlank
     * 
     * @GRID\Column(field="cidade.nome", type="text", title="Cidade", filter="select", selectFrom="source")
     */
    protected $cidade;
    
    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="text", nullable=true)
     */
    protected $email;
    
    /**
     * @var string $email
     *
     * @ORM\Column(name="telefone", type="string", length=45, nullable=true)
     */
    protected $telefone;
    
    /**
     * @var string $email
     *
     * @ORM\Column(name="celular", type="string", length=45, nullable=true)
     */
    protected $celular;

    const TIPO_JURIDICA = 'juridica';
    const TIPO_FISICA = 'fisica';
    /**
     *
     * @var string $tipo
     * @ORM\Column(type="string", length=42, nullable=false)
     */
    protected $tipo;
    
    const SIT_ATIVO = 'Ativo';
    const SIT_INATIVO = 'Inativo';
    /**
     * @var string $situacao
     *
     * @ORM\Column(name="situacao", type="string", nullable=false)
     * @GRID\Column(title="Situação", type="text", align="center", filter="select", selectFrom="source")
     */
    protected $situacao;
    
    /**
     * @var string $permitirVendaCartao
     *
     * @ORM\Column(name="permitir_venda_cartao", type="boolean", nullable=true)
     */
    protected $permitirVendaCartao;
    
    /**
     * @var string $bairro
     *
     * @ORM\Column(name="conta", type="string", length=45, nullable=true)
     */
    protected $conta;
    
    /**
     * @var string $agencia
     *
     * @ORM\Column(name="agencia", type="string", length=45, nullable=true)
     */
    protected $agencia;
    
    /**
     * @var integer $banco
     *
     * @ORM\ManyToOne(targetEntity="Banco", cascade={"persist"}, fetch="LAZY")
     * @ORM\JoinColumn(name="cod_banco", referencedColumnName="cod_banco", onDelete="restrict", nullable=true)
     * 
     */
    protected $banco;
    
    /**
     *
     * @ORM\OneToMany(targetEntity="Telefone", mappedBy="empresa", cascade={"all"})
     */
    protected $telefones;
    
    /**
     * @var string $agencia
     *
     * @ORM\Column(name="dt_notificacao", type="datetime", nullable=true)
     */
    protected $dtNotificacao;
    
    public function __construct() {
        $this->empresaReference = new ArrayCollection();
        $this->listas = new ArrayCollection();
        $this->tipo = self::TIPO_JURIDICA;
        $this->situacao = self::SIT_ATIVO;
        $this->permitirVendaOnline = false;
        $this->dtNotificacao = new \DateTime;
    }
    
    public static function determineValidationGroups(FormInterface $form){
        $data = $form->getData();
        if (self::TIPO_FISICA == $data->getTipo()){
            return array('Default', 'fisica');
        }elseif(self::TIPO_JURIDICA == $data->getTipo()){
            return array('Default', 'juridica');
        }
    }

    public function getTerminais(){
        $terminas = array();
        foreach ($this->getEmpresaReference() as $empresaReference){
            $terminas[] = $empresaReference->getTerminal();
        } 
        
        return $terminas;
    }
    
    /**
     * @ORM\PrePersist
     */
    public function prePersist(){
        if ($this->fantasia == null){
            $this->fantasia = $this->razaoSocial;
        }
    }


    public function getArrayEmails(){
        $emails = explode(';', $this->email);
        $return = array();
        
        foreach ($emails as $email){
            if ($email != "" AND filter_var($email, FILTER_VALIDATE_EMAIL) != false){
                $return[] = trim($email);
            }
        }
        
        return $return;
        
    }

    /**
     * Get codEmpresa
     *
     * @return integer 
     */
    public function getCodEmpresa()
    {
        return $this->codEmpresa;
    }

    /**
     * Set razaoSocial
     *
     * @param string $razaoSocial
     * @return Empresa
     */
    public function setRazaoSocial($razaoSocial)
    {
        $this->razaoSocial = mb_strtoupper($razaoSocial, 'UTF-8');
    
        return $this;
    }

    /**
     * Get razaoSocial
     *
     * @return string 
     */
    public function getRazaoSocial()
    {
        return $this->razaoSocial;
    }
    
    /**
     * Get razaoSocial
     *
     * @return string 
     */
    public function getRazaoSocialMenor()
    {
        if (strlen($this->razaoSocial) > 20){
            return substr($this->razaoSocial, 0, 20)."...";
        }else{
            return substr($this->razaoSocial, 0, 20);
        }
    }

    /**
     * Set fantasia
     *
     * @param string $fantasia
     * @return Empresa
     */
    public function setFantasia($fantasia)
    {
        $this->fantasia = mb_strtoupper($fantasia, 'UTF-8');
    
        return $this;
    }

    /**
     * Get fantasia
     *
     * @return string 
     */
    public function getFantasia()
    {
        return $this->fantasia;
    }

    /**
     * Set cnpj
     *
     * @param string $cnpj
     * @return Empresa
     */
    public function setCnpj($cnpj)
    {
        $this->cnpj = $cnpj;
    
        return $this;
    }

    /**
     * Get cnpj
     *
     * @return string 
     */
    public function getCnpj()
    {
        return $this->cnpj;
    }
    
    public function getCpf() {
        return $this->cpf;
    }

    public function setCpf($cpf) {
        $this->cpf = $cpf;
    }

    public function getTipo() {
        return $this->tipo;
    }

    public function setTipo($tipo) {
        $this->tipo = $tipo;
    }

    /**
     * Set ie
     *
     * @param string $ie
     * @return Empresa
     */
    public function setIe($ie)
    {
        $this->ie = $ie;
    
        return $this;
    }

    /**
     * Get ie
     *
     * @return string 
     */
    public function getIe()
    {
        return $this->ie;
    }

    /**
     * Set im
     *
     * @param string $im
     * @return Empresa
     */
    public function setIm($im)
    {
        $this->im = $im;
    
        return $this;
    }

    /**
     * Get im
     *
     * @return string 
     */
    public function getIm()
    {
        return $this->im;
    }

    /**
     * Set endereco
     *
     * @param string $endereco
     * @return Empresa
     */
    public function setEndereco($endereco)
    {
        $this->endereco = $endereco;
    
        return $this;
    }

    /**
     * Get endereco
     *
     * @return string 
     */
    public function getEndereco()
    {
        return $this->endereco;
    }

    /**
     * Set complemento
     *
     * @param string $complemento
     * @return Empresa
     */
    public function setComplemento($complemento)
    {
        $this->complemento = $complemento;
    
        return $this;
    }

    /**
     * Get complemento
     *
     * @return string 
     */
    public function getComplemento()
    {
        return $this->complemento;
    }

    /**
     * Set cep
     *
     * @param string $cep
     * @return Empresa
     */
    public function setCep($cep)
    {
        $this->cep = $cep;
    
        return $this;
    }

    /**
     * Get cep
     *
     * @return string 
     */
    public function getCep()
    {
        return $this->cep;
    }

    /**
     * Set bairro
     *
     * @param string $bairro
     * @return Empresa
     */
    public function setBairro($bairro)
    {
        $this->bairro = $bairro;
    
        return $this;
    }

    /**
     * Get bairro
     *
     * @return string 
     */
    public function getBairro()
    {
        return $this->bairro;
    }

    /**
     * Set cidade
     *
     * @param integer $cidade
     * @return Empresa
     */
    public function setCidade($cidade)
    {
        $this->cidade = $cidade;
    
        return $this;
    }

    /**
     * Get cidade
     *
     * @return integer 
     */
    public function getCidade()
    {
        return $this->cidade;
    }

    /**
     * Add $empresaReference
     *
     * @param Agp\CoreBundle\Entity\TerminalEmpresa $empresaReference
     */
    public function addEmpresaReference(\Agp\CoreBundle\Entity\TerminalEmpresa $empresaReference)
    {
        $this->empresaReference[] = $empresaReference;
    }

    /**
     * Get empresaReference
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getEmpresaReference()
    {
        return $this->empresaReference;
    }
    
    /**
     * Add $lista
     *
     * @param Agp\CoreBundle\Entity\TerminalEmpresa $lista
     */
    public function addListas(\Agp\CoreBundle\Entity\ListaPreco $lista)
    {
        $this->listas[] = $lista;
    }

    /**
     * Get lista
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getListas()
    {
        return $this->listas;
    }
    
    public function getEmail() {
        return $this->email;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function getTelefone() {
        return $this->telefone;
    }

    public function setTelefone($telefone) {
        $this->telefone = $telefone;
    }

    public function getCelular() {
        return $this->celular;
    }

    public function setCelular($celular) {
        $this->celular = $celular;
    }

        
    public static function getTipoList(){
        return array(self::TIPO_JURIDICA => 'Jurídica',
                     self::TIPO_FISICA => 'Física',);
    }
    
    public function getSituacao() {
        return $this->situacao;
    }

    public function setSituacao($situacao) {
        $this->situacao = $situacao;
    }
    
    public static function getSituacaoList(){
        return array(self::SIT_ATIVO => self::SIT_ATIVO,
                     self::SIT_INATIVO => self::SIT_INATIVO);
    }
    
    public function getPermitirVendaCartao() {
        return $this->permitirVendaCartao;
    }

    public function setPermitirVendaCartao($permitirVendaCartao) {
        $this->permitirVendaCartao = $permitirVendaCartao;
    }
    
    public function getConta() {
        return $this->conta;
    }

    public function setConta($conta) {
        $this->conta = $conta;
    }

    public function getAgencia() {
        return $this->agencia;
    }

    public function setAgencia($agencia) {
        $this->agencia = $agencia;
    }

    public function getBanco() {
        return $this->banco;
    }

    public function setBanco($banco) {
        $this->banco = $banco;
    }
    
    public function getTelefones() {
        return $this->telefones;
    }

    public function setTelefones($telefones) {
        $this->telefones = $telefones;
    }
 
    public function getCelularesSms(){

        $return = array();
        foreach ($this->telefones as $tel){
            if ($tel->getTipo() == Telefone::TP_MOVEL AND $tel->getNotificacao() == true){
                $return[] = $tel;
            }
        }
        
        return $return;
    }
    
    public function getDtNotificacao() {
        return $this->dtNotificacao;
    }

    public function setDtNotificacao($dtNotificacao) {
        $this->dtNotificacao = $dtNotificacao;
    }
    
    public function getNomeSms(){
        $nome = $this->fantasia == null ? $this->razaoSocial : $this->fantasia;
        
        return substr($nome, 0, 15);
    }

}